﻿#include "widget.h"
#include "ui_mainwindow.h"
#include <QDesktopWidget>
#include "preferences.h"

#define TAB_IMAGE	0
#define TAB_RESULT	1
#define TAB_ABOUT	3


//---------------------------------------
MainWindow::MainWindow (const QString &documentPath, QWidget *parent) :
    QMainWindow (parent),
    ui (new Ui::MainWindow)
{
    ui->setupUi (this);

    m_config = new Config (0); // avant toute chose
    m_sessionManager = new SessionManager (Config::sessionsConfPath (), QSettings::NativeFormat, this);
    m_spellChecker = new SpellChecker (this);
    m_fileDialog = new QFileDialog (this);

    setAcceptDrops (true);

    //	Constant	Value	Description
    //	QStyle::SP_TitleBarMinButton	1	Minimize button on title bars (e.g., in QMdiSubWindow).
    //	QStyle::SP_TitleBarMenuButton	0	Menu button on a title bar.
    //	QStyle::SP_TitleBarMaxButton	2	Maximize button on title bars.
    //	QStyle::SP_TitleBarCloseButton	3	Close button on title bars.
    //	QStyle::SP_TitleBarNormalButton	4	Normal (restore) button on title bars.
    //	QStyle::SP_TitleBarShadeButton	5	Shade button on title bars.
    //	QStyle::SP_TitleBarUnshadeButton	6	Unshade button on title bars.
    //	QStyle::SP_TitleBarContextHelpButton	7	The Context help button on title bars.
    //	QStyle::SP_MessageBoxInformation	9	The "information" icon.
    //	QStyle::SP_MessageBoxWarning	10	The "warning" icon.
    //	QStyle::SP_MessageBoxCritical	11	The "critical" icon.
    //	QStyle::SP_MessageBoxQuestion	12	The "question" icon.
    //	QStyle::SP_DesktopIcon	13	The "desktop" icon.
    //	QStyle::SP_TrashIcon	14	The "trash" icon.
    //	QStyle::SP_ComputerIcon	15	The "My computer" icon.
    //	QStyle::SP_DriveFDIcon	16	The floppy icon.
    //	QStyle::SP_DriveHDIcon	17	The harddrive icon.
    //	QStyle::SP_DriveCDIcon	18	The CD icon.
    //	QStyle::SP_DriveDVDIcon	19	The DVD icon.
    //	QStyle::SP_DriveNetIcon	20	The network icon.
    //	QStyle::SP_DirHomeIcon	55	The home directory icon.
    //	QStyle::SP_DirOpenIcon	21	The open directory icon.
    //	QStyle::SP_DirClosedIcon	22	The closed directory icon.
    //	QStyle::SP_DirIcon	37	The directory icon.
    //	QStyle::SP_DirLinkIcon	23	The link to directory icon.
    //	QStyle::SP_FileIcon	24	The file icon.
    //	QStyle::SP_FileLinkIcon	25	The link to file icon.
    //	QStyle::SP_QFileDialogStart	28	The "start" icon in a file dialog.
    //	QStyle::SP_QFileDialogEnd	29	The "end" icon in a file dialog.
    //	QStyle::SP_QFileDialogToParent	30	The "parent directory" icon in a file dialog.
    //	QStyle::SP_QFileDialogNewFolder	31	The "create new folder" icon in a file dialog.
    //	QStyle::SP_QFileDialogDetailedView	32	The detailed view icon in a file dialog.
    //	QStyle::SP_QFileDialogInfoView	33	The file info icon in a file dialog.
    //	QStyle::SP_QFileDialogContentsView	34	The contents view icon in a file dialog.
    //	QStyle::SP_QFileDialogListView	35	The list view icon in a file dialog.
    //	QStyle::SP_QFileDialogBack	36	The back arrow in a file dialog.
    //	QStyle::SP_DockWidgetCloseButton	8	Close button on dock windows (see also QDockWidget).
    //	QStyle::SP_ToolBarHorizontalExtensionButton	26	Extension button for horizontal toolbars.
    //	QStyle::SP_ToolBarVerticalExtensionButton	27	Extension button for vertical toolbars.
    //	QStyle::SP_DialogOkButton	38	Icon for a standard OK button in a QDialogButtonBox.
    //	QStyle::SP_DialogCancelButton	39	Icon for a standard Cancel button in a QDialogButtonBox.
    //	QStyle::SP_DialogHelpButton	40	Icon for a standard Help button in a QDialogButtonBox.
    //	QStyle::SP_DialogOpenButton	41	Icon for a standard Open button in a QDialogButtonBox.
    //	QStyle::SP_DialogSaveButton	42	Icon for a standard Save button in a QDialogButtonBox.
    //	QStyle::SP_DialogCloseButton	43	Icon for a standard Close button in a QDialogButtonBox.
    //	QStyle::SP_DialogApplyButton	44	Icon for a standard Apply button in a QDialogButtonBox.
    //	QStyle::SP_DialogResetButton	45	Icon for a standard Reset button in a QDialogButtonBox.
    //	QStyle::SP_DialogDiscardButton	46	Icon for a standard Discard button in a QDialogButtonBox.
    //	QStyle::SP_DialogYesButton	47	Icon for a standard Yes button in a QDialogButtonBox.
    //	QStyle::SP_DialogNoButton	48	Icon for a standard No button in a QDialogButtonBox.
    //	QStyle::SP_ArrowUp	49	Icon arrow pointing up.
    //	QStyle::SP_ArrowDown	50	Icon arrow pointing down.
    //	QStyle::SP_ArrowLeft	51	Icon arrow pointing left.
    //	QStyle::SP_ArrowRight	52	Icon arrow pointing right.
    //	QStyle::SP_ArrowBack	53	Equivalent to SP_ArrowLeft when the current layout direction is Qt::LeftToRight, otherwise SP_ArrowRight.
    //	QStyle::SP_ArrowForward	54	Equivalent to SP_ArrowRight when the current layout direction is Qt::LeftToRight, otherwise SP_ArrowLeft.
    //	QStyle::SP_CommandLink	56	Icon used to indicate a Vista style command link glyph.
    //	QStyle::SP_VistaShield	57	Icon used to indicate UAC prompts on Windows Vista. This will return a null pixmap or icon on all other platforms.
    //	QStyle::SP_BrowserReload	58	Icon indicating that the current page should be reloaded.
    //	QStyle::SP_BrowserStop	59	Icon indicating that the page loading should stop.
    //	QStyle::SP_MediaPlay	60	Icon indicating that media should begin playback.
    //	QStyle::SP_MediaStop	61	Icon indicating that media should stop playback.
    //	QStyle::SP_MediaPause	62	Icon indicating that media should pause playback.
    //	QStyle::SP_MediaSkipForward	63	Icon indicating that media should skip forward.
    //	QStyle::SP_MediaSkipBackward	64	Icon indicating that media should skip backward.
    //	QStyle::SP_MediaSeekForward	65	Icon indicating that media should seek forward.
    //	QStyle::SP_MediaSeekBackward	66	Icon indicating that media should seek backward.
    //	QStyle::SP_MediaVolume	67	Icon indicating a volume control.
    //	QStyle::SP_MediaVolumeMuted	68	Icon indicating a muted volume control.


    QStyle* style = qApp->style ();

    ui->openImage->setIcon (style->standardIcon (QStyle::SP_DialogOpenButton));
    ui->acquireFromScanner->setIcon (QIcon::fromTheme ("scanner", QIcon (":/images/scanner.svg")));
    ui->rotateLeft->setIcon (QIcon::fromTheme ("object-rotate-left", QIcon (":/images/object-rotate-left.svg")));
    ui->rotateRight->setIcon (QIcon::fromTheme ("object-rotate-right", QIcon (":/images/object-rotate-right.svg")));
    ui->runOCR->setIcon (m_config->appIcon ());
    ui->editRedo->setIcon (QIcon::fromTheme ("edit-redo", QIcon (":/images/edit-redo.svg")));
    ui->editUndo->setIcon (QIcon::fromTheme ("edit-undo", QIcon (":/images/edit-undo.svg")));
    ui->exit->setIcon (QIcon::fromTheme ("application-exit", QIcon (":/images/application-exit.svg")));
    ui->options->setIcon (QIcon::fromTheme ("preferences-desktop", QIcon (":/images/preferences-desktop.svg")));
    ui->help->setIcon (style->standardIcon (QStyle::SP_DialogHelpButton));
    ui->extractFromPDF->setIcon (QIcon::fromTheme ("application-pdf", QIcon (":/images/gnome-mime-application-pdf.svg")));
    ui->zoomBestFit1->setIcon (QIcon::fromTheme ("zoom-best-fit", QIcon (":/images/zoom-best-fit.png")));
    ui->zoomBestFit2->setIcon (QIcon::fromTheme ("zoom-best-fit", QIcon (":/images/zoom-best-fit.png")));
    ui->zoomOriginal1->setIcon (QIcon::fromTheme ("zoom-original", QIcon (":/images/zoom-original.png")));
    ui->zoomOriginal2->setIcon (QIcon::fromTheme ("zoom-original", QIcon (":/images/zoom-original.png")));


    // images
    m_gv1 = ui->graphicsView1;
    m_gv2 = ui->graphicsView2;

    m_pixmapItem2 = new QGraphicsPixmapItem ();
    m_scene2 = new BasicScene ();
    m_scene2->setBackgroundBrush (QBrush (Qt::lightGray));
    m_scene2->addItem (m_pixmapItem2);

    m_scene1 = new Scene (m_pixmapItem2);

    m_gv1->setScene (m_scene1);
    m_gv2->setScene (m_scene2);

    m_document = new Document (m_sessionManager, m_spellChecker, this);


    connect (m_scene1,
             SIGNAL(zoomed(qreal)),
             m_document,
             SLOT(onZoomed1(qreal)));

    connect (m_scene2,
             SIGNAL(zoomed(qreal)),
             m_document,
             SLOT(onZoomed2(qreal)));

    connect (m_document,
             SIGNAL(empty(bool)),
             this,
             SLOT(onDocumentIsEmpty(bool)));

    connect (m_document,
             SIGNAL(zoom1Changed(qreal)),
             m_scene1,
             SLOT(setZoom(qreal)));

    connect (m_document,
             SIGNAL(zoom2Changed(qreal)),
             m_scene2,
             SLOT(setZoom(qreal)));

    connect (m_document,
             SIGNAL (pathChanged (QString)),
             this,
             SLOT(onPathChanged(QString)));

    connect (m_document,
             SIGNAL (originalImageChanged (QPixmap)),
             this,
             SLOT(onOriginalImageChanged(QPixmap)));

    connect (m_document,
             SIGNAL (previewImageChanged (QPixmap)),
             this,
             SLOT(onPreviewImageChanged(QPixmap)));

    connect (m_document,
             SIGNAL(typePdfChanged(bool)),
             this,
             SLOT(onTypePdfChanged(bool)));

    connect (m_document,
             SIGNAL(pageRangeChanged(int,int)),
             this,
             SLOT(onPageRangeChanged(int,int)));

    connect (m_document,
             SIGNAL(currentPageChanged(int)),
             this,
             SLOT(onCurrentPageChanged(int)));

    connect (m_document,
             SIGNAL(rotationChanged(int)),
             this,
             SLOT(onRotationChanged(int)));

    connect (m_document,
             SIGNAL(scaleChanged(double)),
             this,
             SLOT(onScaleChanged(double)));

    connect (m_document,
             SIGNAL(contrastChanged(int)),
             this,
             SLOT(onContrastChanged(int)));

    connect (m_document,
             SIGNAL(optionCleanImageChanged(bool)),
             this,
             SLOT(onOptionCleanImageChanged(bool)));

    connect (m_document,
             SIGNAL(rectsChanged(QList<ImagePart*>,QList<ImagePart*>)),
             m_scene1,
             SLOT(onCreatePartsFrom(QList<ImagePart*>,QList<ImagePart*>)));

    connect(m_scene1,
            SIGNAL(rectsHaveChanged(QList<ImagePart*>,QList<ImagePart*>)),
            m_document,
            SLOT(setRectLists (QList<ImagePart*>,QList<ImagePart*>)));

    connect (m_scene1,
             SIGNAL(askInformationsAboutCurrentImage()),
             m_document,
             SLOT(onAskInformationsAboutCurrentImage()));

    connect (m_scene1,
             SIGNAL(askPartSettingsOf(ImagePart*)),
             m_document,
             SLOT(onAskPartSettingsOf(ImagePart*)));

    connect (ui->pdfPage,
             SIGNAL(changed(int)),
             m_document,
             SLOT(setCurrentPage(int)));

    connect (ui->openImage,
             SIGNAL(clicked()),
             this,
             SLOT(onActionOpen()));

    connect (ui->rotateLeft,
             SIGNAL(clicked()),
             m_document,
             SLOT(setRotationLeft()));

    connect (ui->rotateRight,
             SIGNAL(clicked()),
             m_document,
             SLOT(setRotationRight()));

    connect (m_sessionManager,
             SIGNAL(undoIsPossible(bool)),
             ui->editUndo,
             SLOT(setEnabled(bool)));

    connect (m_sessionManager,
             SIGNAL(redoIsPossible(bool)),
             ui->editRedo,
             SLOT(setEnabled(bool)));

    connect (ui->removeMargins,
             SIGNAL(clicked(bool)),
             m_document,
             SLOT(setOptionCleanPreview(bool)));

    connect (m_scene1,
             SIGNAL(askExtractionFrom(QRectF)),
             this,
             SLOT(onAskExtractionFrom(QRectF)));

    connect (m_scene1,
             SIGNAL(askOCRFrom(ImagePart*)),
             this,
             SLOT(onAskOCRFrom (ImagePart*)));


    ui->scale->setRange (0.1, 3.0);
    ui->scale->setSingleStep (0.1);
    ui->scale->setDecimals (1);
    ui->scale->setValue (1.0);
    ui->scale->setToolTip (tr ("Changer l'échelle"));

    ui->contrast->setRange (1, 99);
    ui->contrast->setSingleStep (1);
    ui->contrast->setValue (50);
    ui->contrast->setToolTip (tr ("Réglage du contraste de l'aperçu"));

    connect (ui->scale,
             SIGNAL(changed(double)),
             m_document,
             SLOT(setScale(double)));

    connect (ui->contrast,
             SIGNAL(changed(int)),
             m_document,
             SLOT(setContrastFromGUI(int)));

    //------------------------------------------
    setWindowTitle (QString ("%1 - version %2")
                    .arg (Config::appName ())
                    .arg (Config::appVersion ()));

    setWindowIcon (Config::appIcon ());
    initMenuScan ();

    clear ();

    if (m_config->optionRememberGeometry ())
    {
        resize (m_config->windowSize ());
        move (m_config->windowPos ());
        if (m_config->windowIsMinimized ())
            showMinimized ();
        if (m_config->windowIsMaximized ())
            showMaximized ();
        ui->splitter->restoreState (m_config->lastSplitterState ());
    }
    else
    {
        // centrage sur l'écran
        QDesktopWidget dw;
        QRect screen (dw.screenGeometry ());
        setGeometry (screen.center ().x () - width () / 2,
                     screen.center ().y () - height () / 2,
                     width (),
                     height ());
    }

    if (!documentPath.isEmpty ())
    {
        open (documentPath);
    }
    else
    {
        if (m_config->optionRestoreWorkSession ())
        {
            m_document->openLastDocument ();
        }
    }

}
//------------------------
MainWindow::~MainWindow ()
{
    delete ui;
    delete m_config;
}
//------------------------------
void MainWindow::closeEvent (QCloseEvent *event)
{
    m_config->setLastGeometry (pos (), size (), isMaximized (), isMinimized ());
    m_config->setLastSplitterState (ui->splitter->saveState ());
    if (m_config->optionRemoveTemp ())
    {
        removeTemp ();
    }
    QMainWindow::closeEvent (event);
}
//-----------------------------------------------------
void MainWindow::dragEnterEvent(QDragEnterEvent *event)
{
    const QMimeData* mimeData = event->mimeData ();

    if (mimeData->hasUrls ())
    {
        event->acceptProposedAction();
    }
}
//--------------------------------------------
void MainWindow::dropEvent(QDropEvent *event)
{
    const QMimeData* mimeData = event->mimeData ();

    if (mimeData->hasUrls ())
    {
        event->acceptProposedAction();
        QList<QUrl> urls (mimeData->urls ());
        open (urls.at (0).toLocalFile ());
    }
}
//-----------------------------
void MainWindow::removeTemp ()
{
    QString workPath (Config::appWorkPath ());
    QDir workDir (workPath);

    QStringList listToRemove (workDir.entryList (QStringList () << "*", QDir::Files | QDir::NoDotAndDotDot));

    foreach (QString fileName, listToRemove)
    {
        QString fullName (workPath + fileName);
        qDebug () << tr ("Suppression de %1").arg (fullName);
        QFile::remove (fullName);
    }
}
//-----------------------------------
void MainWindow::fillOpenMenu ()
{
    QStringList fileList (m_sessionManager->recentFileList (m_config->optionRecentFileMax ())); // limiter la liste

    QMenu* menu = new QMenu (tr ("Documents récents"), this);
    if (!fileList.isEmpty ())
    {
        QList<QAction*> actionList;
        foreach (const QString& fileName, fileList)
        {
            QString type (Config::documentLinuxType (fileName));

            if (type.contains ("image") || type.contains ("pdf"))
            {
                QIcon icon (Config::symbol (fileName));
                QAction* action = new QAction (icon, fileName, this);
                connect (action,
                         SIGNAL(triggered()),
                         this,
                         SLOT(openRecentFile()));
                actionList << action;
            }
        }
        menu->addActions (actionList);
    }
    ui->openImage->setMenu (menu);
    ui->openImage->setPopupMode (QToolButton::MenuButtonPopup);
    ui->openImage->adjustSize ();
}
//--------------------------------
void MainWindow::openRecentFile ()
{
    QAction *action = qobject_cast<QAction *>(sender());
    if (action)
    {
        QString path (action->text ());
        QFileInfo fi (path);
        if (!fi.exists ()) return;
        open (path);
    }
}
//-----------------------
void MainWindow::clear ()
{
    ui->fileName->setText (tr ("(Aucun document ouvert)"));
    ui->preview->setText (tr ("(Aucun aperçu)"));
    m_document->clear ();
    m_gv1->setToolTip (QString ());
    ui->scale->setEnabled (false);
    ui->scale->setValue (1.0);
    ui->contrast->setEnabled (false);
    ui->contrast->setValue (50);
    ui->runOCR->setEnabled (false);
    ui->extractFromPDF->setVisible (false);
    ui->pdfPage->setEnabled (false);
    m_gv1->setTransformationAnchor (QGraphicsView::AnchorViewCenter);
    m_gv1->setTransform (QTransform (), false);
    m_gv2->setTransformationAnchor (QGraphicsView::AnchorViewCenter);
    m_gv2->setTransform (QTransform (), false);
    ui->removeMargins->setChecked (false);
    fillOpenMenu ();
}
//---------------------------------
void MainWindow::onActionOpen ()
{
    m_fileDialog->setAcceptMode (QFileDialog::AcceptOpen);
    m_fileDialog->setViewMode (QFileDialog::List);
    m_fileDialog->setDirectory (m_config->lastOpenDocPath ());
    m_fileDialog->setWindowTitle (tr ("Charger une image ou un PDF"));
    m_fileDialog->setNameFilters (Config::loadFilters ());
    m_fileDialog->setNameFilterDetailsVisible (false);
    m_fileDialog->setFileMode (QFileDialog::ExistingFile);
    if (m_fileDialog->exec () == QDialog::Rejected) return;

    QString imageName = m_fileDialog->selectedFiles ().first ();
    QFileInfo fi (imageName);
    if (!fi.exists ()) return;
    m_config->setLastOpenDocPath (fi.absolutePath ());
    open (imageName);
}

//-----------------------------------
void MainWindow::on_options_clicked()
{
    Preferences preferences (m_sessionManager, this);
    preferences.exec ();
    fillOpenMenu ();
}
//---------------------------------
void MainWindow::on_help_clicked()
{
    m_helpInfoDialog = new HelpInfoDalog (this);
    m_helpInfoDialog->setPage (0);
    m_helpInfoDialog->exec ();
    delete m_helpInfoDialog;
}
//--------------------------------
void MainWindow::on_exit_clicked()
{
    close ();
}
//************************ UNDO ******************************
void MainWindow::on_editUndo_clicked ()
{
    m_document->undo ();
}
//-------------------------------------
void MainWindow::on_editRedo_clicked ()
{
    m_document->redo ();
}
//------------------------------------------------
void MainWindow::on_acquireFromScanner_clicked ()
{
    QString fileName = m_config->scannedImageCompleteFileName ();

    setDisabled (true);
    QApplication::processEvents ();
    int exitCode = QDialog::Accepted;

    if (m_config->optionUseXSane ())
    {
        Xsane* process = new Xsane (this);
        fileName = process->run (fileName);
        delete process;
    }
    else
    {
        Scanner* scanner = new Scanner (fileName, this);
        exitCode = scanner->exec ();
        delete scanner;
    }

    setEnabled (true);
    if (exitCode == QDialog::Accepted)
    {
        m_document->openWorkSession (m_sessionManager->sessionOf (fileName));
    }
}
//-----------------------------------
void MainWindow::initMenuScan ()
{
    QMenu* menu = new QMenu (tr ("Application de numérisation"), this);
    QList<QAction*> actionList;
    QAction* action = new QAction (tr ("Utiliser XSane"), this);
    action->setCheckable (true);
    action->setChecked (m_config->optionUseXSane ());
    connect (action,
             SIGNAL(triggered(bool)),
             m_config,
             SLOT(setOptionUseXSane(bool)));
    actionList << action;
    menu->addActions (actionList);
    ui->acquireFromScanner->setMenu (menu);
    ui->acquireFromScanner->setPopupMode (QToolButton::DelayedPopup);
    ui->acquireFromScanner->adjustSize ();
}
//--------------------------------------------------
void MainWindow::open (const QString &documentPath)
{
    clear ();
    QFileInfo fi (documentPath);
    QString path (fi.canonicalFilePath ());

    Session* s (m_sessionManager->sessionOf (path));
    if (s != 0)
    {
        m_scene1->setZoom (s->zoom1 ());
        m_scene2->setZoom (s->zoom2 ());
    }

    if (!m_document->openWorkSession (s))
    {
        m_sessionManager->removeSession (s->fileName ());
    }
}
//----------------------------------------
void MainWindow::onDocumentIsEmpty(bool b)
{
    ui->runOCR->setEnabled (!b);
    ui->rotateLeft->setEnabled (!b);
    ui->rotateRight->setEnabled (!b);
    ui->scale->setEnabled (!b);
    ui->contrast->setEnabled (!b);
    ui->zoomBestFit1->setEnabled (!b);
    ui->zoomBestFit2->setEnabled (!b);
    ui->zoomOriginal1->setEnabled (!b);
    ui->zoomOriginal2->setEnabled (!b);
    ui->removeMargins->setEnabled (!b);
    QApplication::processEvents ();
}
//-----------------------------------------------------------
void MainWindow::onPathChanged(const QString &documentPath)
{
    if (documentPath.isEmpty ())
    {
        ui->fileName->setText (tr ("(Aucun document ouvert)"));
        ui->preview->setText (tr ("(Aucun aperçu)"));
    }
    else
    {
        ui->fileName->setText (tr ("Document courant : %1").arg (documentPath));
        ui->preview->setText (tr ("Aperçu"));
    }
}
//---------------------------------------------------------------
void MainWindow::onOriginalImageChanged(const QPixmap &original)
{
    m_scene1->setOriginalPixmap (original);
    ui->scale->setEnabled (!original.isNull ());
    ui->contrast->setEnabled (!original.isNull ());
    m_gv1->setToolTip (m_document->originalProperties ());
}
//-------------------------------------------------------------
void MainWindow::onPreviewImageChanged(const QPixmap &preview)
{
    m_scene1->setPreviewPixmap (preview);
    m_gv2->setToolTip (m_document->previewProperties ());
}
//--------------------------------------------------
void MainWindow::onRotationChanged(int /*rotation*/)
{
}
//-------------------------------------------
void MainWindow::onScaleChanged(double scale)
{
    ui->scale->setValue (scale);
}
//-----------------------------------------------
void MainWindow::onContrastChanged(int contrast)
{
    ui->contrast->setValue (contrast);
}
//----------------------------------------------------
void MainWindow::onOptionCleanImageChanged(bool clean)
{
    ui->removeMargins->setChecked (clean);
}
//---------------------------------------
void MainWindow::onTypePdfChanged (bool b)
{
    if (!b)
    {
        ui->pdfPage->setValue (0);
    }
    ui->pdfPage->setVisible (b);
    ui->extractFromPDF->setVisible (b);
    bool PDFcontainsText = m_document->PDFDocContainsText ();
    ui->extractFromPDF->setEnabled (PDFcontainsText);
    m_scene1->setPdf (PDFcontainsText);
}
//-------------------------------------------------------
void MainWindow::onPageRangeChanged(int first, int last)
{
    ui->pdfPage->setMinimum (first);
    ui->pdfPage->setMaximum (last);
    ui->pdfPage->setEnabled (last > first);
    ui->pdfPage->setToolTip (tr ("Ce document comporte %1 pages").arg (last));
}
//--------------------------------------------------
void MainWindow::onCurrentPageChanged(int currentPg)
{
    ui->pdfPage->setValue (currentPg);
}
//----------------------------------
void MainWindow::on_runOCR_clicked()
{
    m_document->onOCRRequired ();
}
//-------------------------------------------
void MainWindow::on_extractFromPDF_clicked ()
{
    m_document->onTextExtractionRequired ();
}
//-------------------------------------------------------
void MainWindow::onAskExtractionFrom(const QRectF& rectF)
{
    m_document->onTextExtractionRequired (rectF);
}
//-------------------------------------------------------
void MainWindow::onAskOCRFrom(ImagePart* part)
{
    m_document->onROIOCRRequired (part);
}
//-------------------------------------------------------
void MainWindow::on_zoomOriginal1_clicked()
{
    m_scene1->setZoomOriginal ();
}
//-------------------------------------------------------
void MainWindow::on_zoomBestFit1_clicked()
{
    m_scene1->setZoomBestFit (m_document->rotatedImage ());
}
//-------------------------------------------------------
void MainWindow::on_zoomOriginal2_clicked()
{
    m_scene2->setZoomOriginal ();
}
//-------------------------------------------------------
void MainWindow::on_zoomBestFit2_clicked()
{
    m_scene2->setZoomBestFit (m_document->previewImage ());
}
//-------------------------------------------------------
